﻿using System;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Reflection;

namespace YJKDataBase
{
	public class SqliteObjectFileTable : IObjectFileTable
	{
		public SqliteObjectFileTable(SqliteDatabase db)
		{
			this.mDatabase = db;
		}

		public bool Add(DbObject family, FileInfo fi, DbTransaction tr)
		{
			byte[] array = Assist.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (DbCommand dbCommand = this.mDatabase.dbCon.CreateCommand())
				{
					dbCommand.Transaction = tr;
					dbCommand.CommandText = string.Format("insert into [ObjectFileTable] ([ObjectGUID],[ObjectFile]) values ('{0}',{1})", family.Guid.ToString(), "@file");
					object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
					{
						"@file",
						array
					}, null, new object[0]);
					dbCommand.Parameters.Add(value);
					dbCommand.ExecuteNonQuery();
				}
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool Remove(DbObject family, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from [ObjectFileTable] where [ObjectGUID]='" + family.Guid.ToString() + "'", tr);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public FileInfo this[DbObject family, string exeName]
		{
			get
			{
				string text = Assembly.GetExecutingAssembly().Location;
				int num = text.LastIndexOf("\\");
				text = text.Substring(0, num + 1);
				FileInfo result = null;
				DataTable dataTable = this.mDatabase.ExecQuery("select [ObjectFile] from [ObjectFileTable] where [ObjectGUID]='" + family.Guid.ToString() + "'");
				DataTable dataTable2 = this.mDatabase.ExecQuery("select [Name] from [ObjectTable] where [GUID]='" + family.Guid.ToString() + "'");
				if (dataTable != null && dataTable2 != null)
				{
					byte[] pReadByte = (byte[])dataTable.Rows[0][0];
					string fileName = string.Concat(new string[]
					{
						text,
						"\\",
						dataTable2.Rows[0][0].ToString(),
						".",
						exeName
					});
					Assist.WriteFile(pReadByte, fileName);
					result = new FileInfo(fileName);
				}
				return result;
			}
		}

		public bool Replace(DbObject family, FileInfo fi)
		{
			DbTransaction dbTransaction = this.mDatabase.StartTransaction();
			byte[] array = Assist.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (DbCommand dbCommand = this.mDatabase.dbCon.CreateCommand())
				{
					dbCommand.Transaction = dbTransaction;
					dbCommand.CommandText = string.Format("update [ObjectFileTable] set [ObjectFile]={0} where [ObjectGUID]='{1}'", "@file", family.Guid.ToString());
					object value = this.mDatabase.Sqlite.CreateInstance(SqliteDatabase.SQLiteParameter, true, BindingFlags.CreateInstance, null, new object[]
					{
						"@file",
						array
					}, null, new object[0]);
					dbCommand.Parameters.Add(value);
					dbCommand.ExecuteNonQuery();
				}
				dbTransaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private SqliteDatabase mDatabase;
	}
}
